package com.thon.entity.user;

// Generated 2013-9-23 19:12:56 by Hibernate Tools 3.4.0.CR1

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.thon.entity.project.Product;
import com.thon.entity.util.IdEntity;

/**
 * MeAttachments generated by hbm2java
 */
@Entity
@Table(name = "attachments")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attachment extends IdEntity {

	private static final long serialVersionUID = 6110981151771907020L;
	private User user;
	private String contentType;
	private Date createdDate;
	private Boolean enabled;
	private Boolean existed;
	private String fileName;
	private Integer fileSize;
	private String fileUrl;
//	private List<Product> products;

	// helper
	private byte[] bytes;
	private String dimension;
	private int width;
	private int height;

	public Attachment() {
		this.enabled = true;
		this.createdDate = new Date();
		this.contentType ="image/jpeg";
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "user_id", nullable = false)
	@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getContentType() {
		return this.contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public Boolean getExisted() {
		return this.existed;
	}

	public void setExisted(Boolean existed) {
		this.existed = existed;
	}

	public String getFileName() {
		return this.fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Integer getFileSize() {
		return this.fileSize;
	}

	public void setFileSize(Integer fileSize) {
		this.fileSize = fileSize;
	}

	public String getFileUrl() {
		return this.fileUrl;
	}

	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}

//	@OneToMany(fetch = FetchType.LAZY, mappedBy = "attachment")
//	public List<Product> getProducts() {
//		return this.products;
//	}
//
//	public void setProducts(List<Product> products) {
//		this.products = products;
//	}

	@Transient
	public String getName() {
		return StringUtils.substringBefore(fileName, ".");
	}

	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getCreatedDate() {
		return this.createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public Boolean getEnabled() {
		return this.enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	@Transient
	public String getSuffix() {
		return "." + StringUtils.substringAfterLast(fileName, ".");
	}

	@Transient
	@JsonIgnore
	public byte[] getBytes() {
		return bytes;
	}

	public void setBytes(byte[] bytes) {
		this.bytes = bytes;
	}

	@Transient
	public String getDimension() {
		return dimension;
	}

	public void setDimension(String dimension) {
		this.dimension = dimension;
	}

	@Transient
	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	@Transient
	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

}
